package com.zjc.yohoshop.mvp.persenter;

import com.zjc.lib_core.mvp.presenter.BasePresenter;
import com.zjc.yohoshop.bean.BrandEntity;
import com.zjc.yohoshop.bean.BrandListEntity;
import com.zjc.yohoshop.mvp.contract.BrandFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.inject.Inject;

import dagger.Provides;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BrandFragmentPresenter extends BasePresenter<BrandFragmentContract.BrandFragmentIView, BrandFragmentContract.BrandFragmentIModel> {

    @Inject
    public BrandFragmentPresenter(BrandFragmentContract.BrandFragmentIView brandFragmentIView, BrandFragmentContract.BrandFragmentIModel brandFragmentIModel) {
        super(brandFragmentIView, brandFragmentIModel);
    }

    /**
     * 负责发起获取品牌列表请求方法
     * */
    public void requestBrand(String menuid){
        JSONObject job = new JSONObject();
        try {
            job.put("menuid",menuid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = job.toString();
        m.brandRequest(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BrandEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(BrandEntity brandEntity) {
                        buildLetters(brandEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.showDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    /**
     * 网络请求回来的品牌列表数据生成通信录数据模式
     * 向fragment传递生成好的数据
     * */
    private void buildLetters(BrandEntity entity){
        //首字母集合
        List<String> letteres = buildLetteres(entity);
        //二级菜单列表集合
        List<BrandListEntity>brandListEntities = buildBrandList(letteres,entity);
        //新入驻品牌集合
        List<BrandEntity.Values> newBrandEntities = buildRecommend(entity);
        //热门品牌集合
        List<BrandEntity.Values> hotBrandEntities = buildHot(entity);
        //推荐品牌集合
        List<BrandEntity.Values> recommendEntities = buildRecommend(entity);
        //广告集合
        List<BrandEntity.Values> bannerEntities = buildRecommend(entity);
        //向BrandFragment回调的Map
        Map<String,List> map = new HashMap<>();
        map.put("letteres",letteres);
        map.put("brandListEntities",brandListEntities);
        map.put("newBrandEntities",newBrandEntities);
        map.put("hotBrandEntities",hotBrandEntities);
        map.put("recommendEntities",recommendEntities);
        map.put("bannerEntities",bannerEntities);
        v.brandSuccess(map);
    }

    //提取letters首字母集合方法
    private List<String> buildLetteres(BrandEntity entity){
        List<String>letters = new ArrayList<>();
        for (BrandEntity.Values value: entity.values){
            letters.add(value.brand_letter);
        }
        //首字母去重
        Set<String> set = new TreeSet<>(letters);
        letters.clear();
        letters.addAll(set);
        Collections.sort(letters);
        return letters;
    }

    //构建二级列表方法
    private List<BrandListEntity> buildBrandList( List<String>letteres,BrandEntity entity){
        List<BrandListEntity> listEntities = new ArrayList<>();
        for (String letter : letteres){
            BrandListEntity listEntity = new BrandListEntity();
            listEntity.brand_letter = letter;
            listEntity.setType(1);
            listEntities.add(listEntity);
            for (BrandEntity.Values values : entity.values){
                if (listEntity.brand_letter.equals(values.brand_letter)){
                    BrandListEntity child = new BrandListEntity();
                    child.setType(2);
                    child.brand_name = values.brand_name;
                    child.hot_tag = values.hot_tag;
                    child.brand_id = values.brand_id;
                    child.brand_bg = values.brand_bg;
                    child.brand_icon = values.brand_icon;
                    child.brand_time = values.brand_time;
                    listEntities.add(child);
                }
            }
        }
        return listEntities;
    }

    //构建热门品牌列表方法
    private List<BrandEntity.Values>buildHot(BrandEntity entity){
        List<BrandEntity.Values> hotList = new ArrayList<>();
        for (BrandEntity.Values values : entity.values){
            if (values.hot_tag.equals("true")){
                hotList.add(values);
            }
        }
        return hotList;
    }

    //构建推荐
    private List<BrandEntity.Values>buildRecommend(BrandEntity entity){
        List<BrandEntity.Values>list = new ArrayList<>();
        for (int i = 0;i < entity.values.size();i ++){
            list.add(entity.values.get(i));
            if (i == 8){
                break;
            }
        }
        return list;
    }

}
